package com.leyou.order.web;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import com.leyou.order.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderDTO  订单的DTO接收页面传过来的参数
     * @return  订单ID
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody @Valid OrderDTO orderDTO){
        Long orderId = orderService.createOrder(orderDTO);
        return ResponseEntity.ok(orderId);
    }

    /**
     * 根据订单编号查询订单详情
     * @param id    订单id
     * @return      订单VO
     */
    @GetMapping("/{id}")
    public ResponseEntity<OrderVO> queryOrderById(@PathVariable("id") Long id){
        OrderVO ov = orderService.queryOrderById(id);
        return ResponseEntity.ok(ov);
    }

    /**
     * 获取微信支付的URL
     * @param id    订单的编号
     * @return      返回微信支付的链接
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> queryPayUrl(@PathVariable("id") Long id){
        return ResponseEntity.ok(orderService.queryPayUrl(id));
    }

    /**
     * 检测订单状态
     * @param id ：  1或者其他
     * @return
     */
    @GetMapping("/state/{id}")
    public ResponseEntity<Integer> checkOrderState(@PathVariable("id") Long id){
        return ResponseEntity.ok(orderService.checkOrderState(id));
    }

}
